package com.example.customview.view

import android.content.Context
import android.graphics.Camera
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.withSave
import com.example.customview.R
import com.example.customview.util.getBitmap
import com.sample.base.utils.dp

class CameraViewAnimator(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    companion object {
        private val BITMAP_SIZE = 200.dp
    }


    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val bitmap = this.getBitmap(BITMAP_SIZE, R.drawable.avatar_rengwuxian)

    private val camera = Camera().apply {
        rotateX(rotateAngle)
        // 移动 camera 的位置，使其在不同的手机上表现差不多
        setLocation(0f, 0f, -6 * resources.displayMetrics.density)
    }

    var topFlip = 0f
        set(value) {
            field = value
            invalidate()
        }
    var bottomFlip = 0f
        set(value) {
            field = value
            invalidate()
        }
    var rotateAngle = 0f
        set(value) {
            field = value
            invalidate()
        }

    override fun onDraw(canvas: Canvas) {
        // 确定中心点
        val paddingLeft = width / 2f - BITMAP_SIZE / 2
        val paddingHeight = height / 2f - BITMAP_SIZE / 2

        // 先裁切上半部分
        canvas.withSave {
            // 再移到中间
            canvas.translate(
                (paddingLeft + BITMAP_SIZE / 2),
                (paddingHeight + BITMAP_SIZE / 2)
            )
            //旋转回去
            canvas.rotate(-rotateAngle)
            camera.save()
            camera.rotateX(topFlip)
            camera.applyToCanvas(canvas)
            camera.restore()
            // 裁切 范围要扩大一点
            canvas.clipRect(
                -BITMAP_SIZE.toFloat(),
                -BITMAP_SIZE.toFloat(),
                BITMAP_SIZE.toFloat(),
                0f
            )
            //旋转
            canvas.rotate(rotateAngle)

            // 移动到 view 的原点 （0，0）的位置
            canvas.translate(
                -(paddingLeft + BITMAP_SIZE / 2),
                -(paddingHeight + BITMAP_SIZE / 2)
            )
            // draw bitmap 绘制在中心
            canvas.drawBitmap(
                bitmap,
                paddingLeft,
                paddingHeight,
                paint
            )
        }

        // 裁切下半部分
        canvas.withSave {
            // 再移到中间
            canvas.translate(
                (paddingLeft + BITMAP_SIZE / 2),
                (paddingHeight + BITMAP_SIZE / 2)
            )
            //旋转回去
            canvas.rotate(-rotateAngle)

            camera.save()
            camera.rotateX(bottomFlip)
            camera.applyToCanvas(canvas)
            camera.restore()

//            camera.applyToCanvas(canvas)
            // 裁切 范围要扩大一点
            canvas.clipRect(
                -BITMAP_SIZE.toFloat(),
                0f,
                BITMAP_SIZE.toFloat(),
                BITMAP_SIZE.toFloat()
            )
            //旋转
            canvas.rotate(rotateAngle)

            // 移动到 view 的原点 （0，0）的位置
            canvas.translate(
                -(paddingLeft + BITMAP_SIZE / 2),
                -(paddingHeight + BITMAP_SIZE / 2)
            )
            // draw bitmap 绘制在中心
            canvas.drawBitmap(
                bitmap,
                paddingLeft,
                paddingHeight,
                paint
            )
        }

    }

}